package com.marswin89.marsdaemon.demo;

import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

/**
 * Created by xuxingcan on 16-7-31.
 */
public class LocationUtil {
    private static double lastLatitude = -1;
    private static double lastLongitude = -1;

    public static void getLocation(final Context context) {
        //声明mLocationOption对象
        final AMapLocationClient mlocationClient = new AMapLocationClient(context);
//初始化定位参数
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
//设置定位监听
        mlocationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation amapLocation) {
                mlocationClient.stopLocation();
                if (amapLocation != null) {
                    if (amapLocation.getErrorCode() == 0) {
                        //定位成功回调信息，设置相关消息
                        amapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
                        lastLatitude = amapLocation.getLatitude();//获取纬度
                        lastLongitude = amapLocation.getLongitude();//获取经度
//                        amapLocation.getAccuracy();//获取精度信息
                        FileUtil.getInstance().writeFileSdcardFile("纬度:" + amapLocation.getLatitude() + "\n经度:" + amapLocation.getLongitude() + "\n\n\n");
                        HttpUtil.getInstance().sendGet("http://139.196.106.232:8888/WebForm3.aspx?Latitude=" + amapLocation.getLatitude() + "&Longitude=" + amapLocation.getLongitude() + "&code=" + WID.getWid(context));
                        FileUtil.getInstance().writeLog("sendGet1\n\n\n");
                    } else {
                        //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                        Log.e("AmapError", "location Error, ErrCode:"
                                + amapLocation.getErrorCode() + ", errInfo:"
                                + amapLocation.getErrorInfo());
                        if (lastLatitude != -1 && lastLongitude != -1) {
                            HttpUtil.getInstance().sendGet("http://139.196.106.232:8888/WebForm3.aspx?Latitude=" + lastLatitude + "&Longitude=" + lastLongitude + "&code=" + WID.getWid(context));
                            FileUtil.getInstance().writeLog("sendGet2\n\n\n");
                        }
                    }
                } else {
                    if (lastLatitude != -1 && lastLongitude != -1) {
                        HttpUtil.getInstance().sendGet("http://139.196.106.232:8888/WebForm3.aspx?Latitude=" + lastLatitude + "&Longitude=" + lastLongitude + "&code=" + WID.getWid(context));
                        FileUtil.getInstance().writeLog("sendGet3\n\n\n");
                    }
                }
            }
        });
//设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2 * 60 * 1000);
//设置定位参数
        mlocationClient.setLocationOption(mLocationOption);
// 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
// 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
// 在定位结束后，在合适的生命周期调用onDestroy()方法
// 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
//启动定位
        mlocationClient.startLocation();
    }
}
